package com.digitalchina.lxnsy.activity.old;

import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessOrders;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.OrderListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.OrderListHolder;
import com.example.zhouwei.library.CustomPopWindow;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/1/18 10:46
 * 修改备注：
 * ================================================
 */
public class OrderListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.order_list_recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private List<OrderListVO> orderListVOList = new ArrayList<>();
    private CustomPopWindow mCustomPopWindow;

    private ImageView ivTitleImage;
    private int type = 0; // 0-全部  1-待支付  2-待评价  3-已关闭

    @Override
    public void setLayout() {
        setTitle("全部订单");
        setContentLayout(R.layout.activity_order_list);
    }

    @Override
    public void initMember() {

        ivTitleImage = (ImageView) findViewById(R.id.actionbar_iv_title_image);
        ivTitleImage.setVisibility(View.VISIBLE);
        ivTitleImage.setImageResource(R.drawable.ic_down);

        initRecyclerView();

        initPopWindow();
    }

    /**
     * 顶部订单切换的pop_window
     */
    private void initPopWindow() {

        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_window_order_type, null);

        //处理popWindow 显示内容
        handleLogic(contentView);

        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)//显示的布局，还可以通过设置一个View
                .size(WindowManager.LayoutParams.MATCH_PARENT, Utils.dip2px(this, 60))
                .setFocusable(true)//是否获取焦点，默认为ture
                .setOutsideTouchable(true)//是否PopupWindow 以外触摸dissmiss
                .setOnDissmissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams params = getWindow().getAttributes();
                        params.alpha = 1f;
                        getWindow().setAttributes(params);
                        ivTitleImage.setImageResource(R.drawable.ic_down);
                    }
                })
                .create();//创建PopupWindow
    }

    /**
     * 显示pop_window
     */
    private void showPopWindow() {
        mCustomPopWindow.showAsDropDown(mViewGroupActionBar, 0, 0);//显示PopupWindow
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.7f;
        getWindow().setAttributes(params);
        ivTitleImage.setImageResource(R.drawable.ic_up);
    }

    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleLogic(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCustomPopWindow != null) {
                    mCustomPopWindow.dissmiss();
                }
                switch (v.getId()) {
                    case R.id.pop_order_tv_all:
                        type = 0;
                        break;
                    case R.id.pop_order_tv_wait_pay:
                        type = 1;
                        break;
                    case R.id.pop_order_tv_wait_evaluate:
                        type = 2;
                        break;
                    case R.id.pop_order_tv_close:
                        type = 3;
                        break;
                }
                adapter.clear();
                setTitleByType(type);
            }
        };
        contentView.findViewById(R.id.pop_order_tv_all).setOnClickListener(listener);
        contentView.findViewById(R.id.pop_order_tv_wait_pay).setOnClickListener(listener);
        contentView.findViewById(R.id.pop_order_tv_wait_evaluate).setOnClickListener(listener);
        contentView.findViewById(R.id.pop_order_tv_close).setOnClickListener(listener);
    }

    /**
     * 根据选择类型设置对应的title和访问服务
     *
     * @param type
     */
    private void setTitleByType(int type) {

        switch (type) {
            case 0:
                setTitle("全部订单");
                break;
            case 1:
                setTitle("待支付");
                break;
            case 2:
                setTitle("待评价");
                break;
            case 3:
                setTitle("已关闭");
                break;
        }

        onRefresh();
    }

    private void initRecyclerView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new OrderListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                GotoUtil.gotoActivity(OrderListActivity.this, OrdersDetailsActivity.class, "OrderListVO", adapter.getAllData().get(position));
            }
        });

        mTextViewTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });

        ivTitleImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });
    }

    @Override
    public void setHandler() {


        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null)
                    return;

                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_GET_ORDER_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_ORDER_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_GET_ORDER_BY_STATUS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_ORDER_BY_STATUS_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        orderListVOList = FastJsonUtil.getListBean(object.toString(), "body", "orderList", OrderListVO.class);
        if (page == 1)  // 刷新
            adapter.clear();

        if (orderListVOList != null)
            if (orderListVOList.size() < limit)
                adapter.stopMore();


        adapter.addAll(orderListVOList);

    }

    @Override
    public void rightOnClick() {

    }


    @Override
    public void onRefresh() {
        page = 1;
        getOrderList();
    }

    private void getOrderList() {


        switch (type) {
            case 0: // 全部订单
                BusinessOrders.getOrderList(this, String.valueOf(page), String.valueOf(limit), mHandler);
                break;
            case 1: // 待支付
                BusinessOrders.getOrderByStatus(this, "29", String.valueOf(page), String.valueOf(limit), mHandler);
                break;
            case 2: // 待评价
                BusinessOrders.getOrderByStatus(this, "22", String.valueOf(page), String.valueOf(limit), mHandler);
                break;
            case 3: // 已关闭
                BusinessOrders.getOrderByStatus(this, "23", String.valueOf(page), String.valueOf(limit), mHandler);
                break;
        }
    }

    @Override
    public void onLoadMore() {
            page++;
            getOrderList();
    }
}
